//
//  XDChangePasswordController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/3/10.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDChangePasswordController.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDVerifiedViewController.h"
#define kAlphaNum @"Xx0123456789"
#import "ZHRTRequestFailureShowViewOrDetail.h"
#import "ZHRTBasicMethod.h"
#import "XDChangePasswordVerController.h"
@interface XDChangePasswordController ()
@property (nonatomic,weak) UITextField *cardTf;

@property (nonatomic, weak) UIButton *nextBtn;

@property (nonatomic, copy) NSString *mobile;

@end

@implementation XDChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改交易密码";
    self.view.backgroundColor = kColorRGB(234, 234, 234);
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"pass_Tel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(passwordTel)];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nameTextFeildEditChanged:)
                                                name:UITextFieldTextDidChangeNotification object:self.cardTf];
    [self setUI];
    
   
}

- (void)setUI {
    
   UITextField *cardTf = [self createTextFieldWithFrame:CGRectMake(0, 10, kScreenW, 50) textFont:kBaseFont_(16) textColor:kColorRGB(51, 51, 51) placeholder:@"请输入真实身份证号码" placeholderFont:kBaseFont_(13) leftTitle:@"身份证号" delegate:self inView:self.view tag:0];
    self.cardTf = cardTf;
    
    UIImage *img = [UIImage imageNamed:@"next_fill_hieght"];
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenW - img.size.width) * 0.5 ,CGRectGetMaxY(cardTf.frame) + 38 , img.size.width, img.size.height)];
    [nextBtn setBackgroundImage:img forState:UIControlStateNormal];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"next_fill_nor"] forState:UIControlStateDisabled];
    nextBtn.enabled = NO;
    [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    self.nextBtn = nextBtn;

    
    
}
//判断是否是数字，不是的话就输入失败
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if (textField == self.cardTf) {
        
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
        BOOL canChange = [string isEqualToString:filtered];
        if (!canChange) {
            
            [ZHRTRequestFailureShowViewOrDetail showViewDetail:@"请输入真实的身份证号码" view:self.view];
            
        }
        
        return canChange;
    }
    return YES;
}

- (void)nameTextFeildEditChanged:(id)notification{
    
   
    NSString *cardTfText = [[self.cardTf.text stringByReplacingOccurrencesOfString:@" " withString:@""]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSMutableString *insertStr = [NSMutableString stringWithString:cardTfText];
    
    if (cardTfText.length > 18) {
        [ZHRTRequestFailureShowViewOrDetail showViewDetail:@"请输入真实的身份证号码" view:self.view];
        self.cardTf.text = [self.cardTf.text stringByReplacingCharactersInRange:NSMakeRange(self.cardTf.text.length - 1, 1) withString:@""];
        cardTfText = self.cardTf.text;
    }
    
    if (cardTfText.length == 18) {
            for (NSInteger i = 0; i < cardTfText.length; i ++) {
                if (i == 6) {
                    [insertStr insertString:@" " atIndex:6];
                }
                
                if (i == 14) {
                    [insertStr insertString:@" " atIndex:15];
                }
            self.cardTf.text = insertStr;
        }
        
        
    } else {
        self.cardTf.text = cardTfText;
    }
    
    if ([ZHRTBasicMethod checkIdentityCardNo:cardTfText]) {
        self.nextBtn.enabled = YES;
    } else {
        self.nextBtn.enabled = NO;
    }
}

//修改UITextField的placeholder字体并垂直居中
- (NSAttributedString *)textFieldAttributedPlaceholderWith:(UITextField *)textField placeholder:(NSString *)placeholder placeholderFont:(UIFont *)placeholderFont  {
    NSMutableParagraphStyle *style = [textField.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
    style.minimumLineHeight = textField.font.lineHeight - (textField.font.lineHeight - placeholderFont.lineHeight) / 2.0;
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSFontAttributeName:placeholderFont, NSParagraphStyleAttributeName : style}];
    return attr;
}

- (UITextField *)createTextFieldWithFrame:(CGRect)frame textFont:(UIFont *)textFont textColor:(UIColor *)textColor placeholder:(NSString *)placeholder placeholderFont:(UIFont *)placeholderFont leftTitle:(NSString *)leftTitle delegate:(id)obj inView:(UIView *)inView tag:(NSInteger)tag {
    
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    
    textField.background = [UIImage imageNamed:@"textfield-1"];
    textField.textAlignment = NSTextAlignmentRight;
    textField.textColor =  textColor;
    textField.font = textFont;
    textField.tag = tag;
    
    textField.attributedPlaceholder = [self textFieldAttributedPlaceholderWith:textField placeholder:placeholder placeholderFont:placeholderFont];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 80, 48)];
    leftView.backgroundColor = [UIColor whiteColor];
    
    UILabel *nameLb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 80, 48)];
    nameLb.text = leftTitle;
    nameLb.textColor = kColorRGB(102,102,102);
    nameLb.font = kBaseFont_(14);
    nameLb.textAlignment = NSTextAlignmentLeft;
    [leftView addSubview:nameLb];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    textField.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 15, 48)];
    textField.rightViewMode = UITextFieldViewModeAlways;
    
    textField.delegate = obj;
    [inView addSubview:textField];
    return textField;
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)nextBtnClick:(UIButton *)nextBtn {
    
    [self requestData];

}

- (void)requestData {
    
    NSString *idNumber = [[self.cardTf.text stringByReplacingOccurrencesOfString:@" " withString:@""]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSDictionary * param = @{@"method":@"modifySafePass",@"idNumber":idNumber};
    
        [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
            
            
            if ([responseObject[@"code"] isEqualToString:@"100000"]) {
//                {
//                    code = 100000;
//                    message = "\U64cd\U4f5c\U6210\U529f";
//                    result =     {
//                        mobile = 13240069415;
//                    };
//                    type = success;
//                }

                self.mobile =  responseObject[@"result"][@"mobile"];
                
                XDChangePasswordVerController *changePasswordVer  = [[XDChangePasswordVerController alloc] init];
                changePasswordVer.mobile = self.mobile;
                [self.navigationController pushViewController:changePasswordVer animated:YES];
                
                    }  else {
                        [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
                    }
                } failure:^(NSURLSessionDataTask *task, NSError *error) {
                    [ZHRTRequestFailureShowViewOrDetail showViewDetail:@"无网络" view:self.view];
                }];
    
}

- (void)passwordTel {
    
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"400-805-9696"];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
 
}


@end
